---
title: "R 包 tibble 学习"
author: "宇飞的世界"
date: "2021-06-28"
output: 
  html_document:
    toc: true
    toc_float: true
---

<script src="{{< blogdown/postref >}}index_files/accessible-code-block/empty-anchor.js"></script>
<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  background-color: #f8f8f8; }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ef2929; } /* Alert */
code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #c4a000; } /* Attribute */
code span.bn { color: #0000cf; } /* BaseN */
code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4e9a06; } /* Char */
code span.cn { color: #000000; } /* Constant */
code span.co { color: #8f5902; font-style: italic; } /* Comment */
code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code span.dt { color: #204a87; } /* DataType */
code span.dv { color: #0000cf; } /* DecVal */
code span.er { color: #a40000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #0000cf; } /* Float */
code span.fu { color: #000000; } /* Function */
code span.im { } /* Import */
code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code span.ot { color: #8f5902; } /* Other */
code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code span.sc { color: #000000; } /* SpecialChar */
code span.ss { color: #4e9a06; } /* SpecialString */
code span.st { color: #4e9a06; } /* String */
code span.va { color: #000000; } /* Variable */
code span.vs { color: #4e9a06; } /* VerbatimString */
code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
</style>

<div id="TOC">
<ul>
<li><a href="#tibble-介绍">tibble 介绍</a><ul>
<li><a href="#安装">安装</a></li>
</ul></li>
<li><a href="#创建-tibble">创建 tibble</a></li>
<li><a href="#tibble-的数据操作">tibble 的数据操作</a></li>
<li><a href="#tibbles-and-data.frame-区别">Tibbles and data.frame 区别</a><ul>
<li><a href="#打印">打印</a></li>
<li><a href="#子集">子集</a></li>
</ul></li>
<li><a href="#tibble-转换成-data.frame">tibble 转换成 data.frame</a></li>
<li><a href="#参考资料">参考资料</a></li>
</ul>
</div>

<html>
<style> 
.hljs-comment{
  font-family: Microsoft YaHei ;
  font-style: normal;
}
</style>
</html>
<div id="tibble-介绍" class="section level2">
<h2>tibble 介绍</h2>
<p>tibble 是 R 语言中用来替换 data.frame 数据类型的扩展数据框，tibble 继承于 data.frame ,是弱类型(tibble 是 data.frame 的子类型),tibble 与data.frame 有相同的语法，但是使用起来更加方便。tibble 包由 Hadley 开发的 R包，现在集成在 tidyverse系列中。用作者的话说， R 语言是一门古老的语言，以前有用的特性现在说不定变成了羁绊，但是在不破坏现在代码的情况下改进 R 是很难的，所以大部分的创新都发生在 R中包，这也是 tibble 产生的原因，是 tidyverse 系列 R 包的基础数据框架，让我们使用处理数据更加轻松。</p>
<p>对于 tibble 包，我们只需要掌握创建，数据类型，数据查看，数据操作与data.frame 的区别，关于具体的数据处理，我们使用 dplyr 或 tidyr 完成。</p>
<p><strong>了解 tibble 也不影响使用 tidyverse 系列 R 包处理数据</strong>。</p>
<div id="安装" class="section level3">
<h3>安装</h3>
<p>由于 tibble 集成在 tidyverse 中，所以我们安装加载 tidyverse 即可。</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">library</span>(tidyverse,<span class="dt">warn.conflicts =</span> <span class="ot">TRUE</span>)</span>
<span id="cb1-2"><a href="#cb1-2"></a><span class="co"># library(tibble)</span></span></code></pre></div>
</div>
</div>
<div id="创建-tibble" class="section level2">
<h2>创建 tibble</h2>
<p>使用 tidyverse 系列 R 包的函数(dplyr)生成的数据类型都是『tibbles』，由于 tibbles 是tidyverse系列的底层数据格式，但是大多数其他 R 包使用常规的 data.frame 类型，所以我们可能需要将 data.frame 转换成 tibble，函数是<code>as_tibble()</code>:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1"></a><span class="co"># tibble 包函数，但是已经被启用了</span></span>
<span id="cb2-2"><a href="#cb2-2"></a><span class="co"># as.tibble(iris)</span></span>
<span id="cb2-3"><a href="#cb2-3"></a></span>
<span id="cb2-4"><a href="#cb2-4"></a><span class="co"># 改用 dplyr 中的as_tibble()</span></span>
<span id="cb2-5"><a href="#cb2-5"></a><span class="kw">as_tibble</span>(iris)</span>
<span id="cb2-6"><a href="#cb2-6"></a><span class="co">## # A tibble: 150 x 5</span></span>
<span id="cb2-7"><a href="#cb2-7"></a><span class="co">##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species</span></span>
<span id="cb2-8"><a href="#cb2-8"></a><span class="co">##           &lt;dbl&gt;       &lt;dbl&gt;        &lt;dbl&gt;       &lt;dbl&gt; &lt;fct&gt;  </span></span>
<span id="cb2-9"><a href="#cb2-9"></a><span class="co">##  1          5.1         3.5          1.4         0.2 setosa </span></span>
<span id="cb2-10"><a href="#cb2-10"></a><span class="co">##  2          4.9         3            1.4         0.2 setosa </span></span>
<span id="cb2-11"><a href="#cb2-11"></a><span class="co">##  3          4.7         3.2          1.3         0.2 setosa </span></span>
<span id="cb2-12"><a href="#cb2-12"></a><span class="co">##  4          4.6         3.1          1.5         0.2 setosa </span></span>
<span id="cb2-13"><a href="#cb2-13"></a><span class="co">##  5          5           3.6          1.4         0.2 setosa </span></span>
<span id="cb2-14"><a href="#cb2-14"></a><span class="co">##  6          5.4         3.9          1.7         0.4 setosa </span></span>
<span id="cb2-15"><a href="#cb2-15"></a><span class="co">##  7          4.6         3.4          1.4         0.3 setosa </span></span>
<span id="cb2-16"><a href="#cb2-16"></a><span class="co">##  8          5           3.4          1.5         0.2 setosa </span></span>
<span id="cb2-17"><a href="#cb2-17"></a><span class="co">##  9          4.4         2.9          1.4         0.2 setosa </span></span>
<span id="cb2-18"><a href="#cb2-18"></a><span class="co">## 10          4.9         3.1          1.5         0.1 setosa </span></span>
<span id="cb2-19"><a href="#cb2-19"></a><span class="co">## # … with 140 more rows</span></span></code></pre></div>
<p>我们还可以像创建常规数据框 data.frame 的方式创建新的 tibble。tibble() 将自动循环增长输入长度为 1 的变量，允许引用刚刚创建的其他变量的长度。如下所示：</p>
<blockquote>
<p>原文：tibble() will automatically recycle inputs of length 1, and allows you to refer to variables that you just created, as shown below.</p>
</blockquote>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1"></a><span class="kw">tibble</span>(</span>
<span id="cb3-2"><a href="#cb3-2"></a>  <span class="dt">x =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">5</span>, </span>
<span id="cb3-3"><a href="#cb3-3"></a>  <span class="dt">y =</span> <span class="dv">1</span>, </span>
<span id="cb3-4"><a href="#cb3-4"></a>  <span class="dt">z =</span> x <span class="op">^</span><span class="st"> </span><span class="dv">2</span> <span class="op">+</span><span class="st"> </span>y</span>
<span id="cb3-5"><a href="#cb3-5"></a>)</span>
<span id="cb3-6"><a href="#cb3-6"></a><span class="co">## # A tibble: 5 x 3</span></span>
<span id="cb3-7"><a href="#cb3-7"></a><span class="co">##       x     y     z</span></span>
<span id="cb3-8"><a href="#cb3-8"></a><span class="co">##   &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb3-9"><a href="#cb3-9"></a><span class="co">## 1     1     1     2</span></span>
<span id="cb3-10"><a href="#cb3-10"></a><span class="co">## 2     2     1     5</span></span>
<span id="cb3-11"><a href="#cb3-11"></a><span class="co">## 3     3     1    10</span></span>
<span id="cb3-12"><a href="#cb3-12"></a><span class="co">## 4     4     1    17</span></span>
<span id="cb3-13"><a href="#cb3-13"></a><span class="co">## 5     5     1    26</span></span></code></pre></div>
<p>也就是 y 变量输入的长度仅为 1，但是会参考其他变量自动循环增长为相同长度的向量。</p>
<p>tibble 特性如下：</p>
<ul>
<li>永远不会更改输入的类型，例如永远不会将字符串转换为因子</li>
<li>永远不会更改变量的名称</li>
<li>永远不会创建行名称</li>
<li>tibble 的列名可能是特殊字符，也就是<strong>non-syntactic </strong>的名称</li>
</ul>
<p>例如特殊符号:) 或空格的变量名称，要引用这些变量名称，我们需要使用符号 `(反引号,Tab 键上面)包裹：</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1"></a>tb &lt;-<span class="st"> </span><span class="kw">tibble</span>(</span>
<span id="cb4-2"><a href="#cb4-2"></a>  <span class="st">`</span><span class="dt">:)</span><span class="st">`</span> =<span class="st"> &quot;smile&quot;</span>, </span>
<span id="cb4-3"><a href="#cb4-3"></a>  <span class="st">`</span><span class="dt"> </span><span class="st">`</span> =<span class="st"> &quot;space&quot;</span>,</span>
<span id="cb4-4"><a href="#cb4-4"></a>  <span class="st">`</span><span class="dt">2000</span><span class="st">`</span> =<span class="st"> &quot;number&quot;</span></span>
<span id="cb4-5"><a href="#cb4-5"></a>)</span></code></pre></div>
<p>创建 tibble 的另一种方法是使用<code>tribble()</code>，是一种方便阅读的方式输入少量数据的方式。</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1"></a><span class="kw">tribble</span>(</span>
<span id="cb5-2"><a href="#cb5-2"></a>  <span class="op">~</span>name, <span class="op">~</span>grade, <span class="op">~</span>num,</span>
<span id="cb5-3"><a href="#cb5-3"></a>  <span class="st">&quot;a&quot;</span>, <span class="dv">2</span>, <span class="fl">3.6</span>,</span>
<span id="cb5-4"><a href="#cb5-4"></a>  <span class="st">&quot;b&quot;</span>, <span class="dv">1</span>, <span class="fl">8.5</span>,</span>
<span id="cb5-5"><a href="#cb5-5"></a>  <span class="st">&quot;d&quot;</span>,<span class="dv">3</span>,<span class="dv">3</span></span>
<span id="cb5-6"><a href="#cb5-6"></a>)</span>
<span id="cb5-7"><a href="#cb5-7"></a><span class="co">## # A tibble: 3 x 3</span></span>
<span id="cb5-8"><a href="#cb5-8"></a><span class="co">##   name  grade   num</span></span>
<span id="cb5-9"><a href="#cb5-9"></a><span class="co">##   &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb5-10"><a href="#cb5-10"></a><span class="co">## 1 a         2   3.6</span></span>
<span id="cb5-11"><a href="#cb5-11"></a><span class="co">## 2 b         1   8.5</span></span>
<span id="cb5-12"><a href="#cb5-12"></a><span class="co">## 3 d         3   3</span></span></code></pre></div>
<p>当我们创建 tibble 后，我们可以通过输出可以看到 tibble 返回每列的类型。tibble 定义了 7 种数据类型：</p>
<p><span class="math inline">\(\rightarrow\)</span> int, 代表 integer 整型</p>
<p><span class="math inline">\(\rightarrow\)</span> dbl, 代表 double 实数 双精度浮点型</p>
<p><span class="math inline">\(\rightarrow\)</span> chr, 代表 character 字符型</p>
<p><span class="math inline">\(\rightarrow\)</span> dttm, 代表 日期+时间(date + time)</p>
<p><span class="math inline">\(\rightarrow\)</span> lgl, 代表 逻辑判断 TRUE 或 FALSE</p>
<p><span class="math inline">\(\rightarrow\)</span> fctr, 代表 因子类型 factor</p>
<p><span class="math inline">\(\rightarrow\)</span> date, 代表 日期 dates</p>
<blockquote>
<p>R 没有单精度数据类型，所有实数都以双精度格式存储</p>
</blockquote>
<p>查看<code>tibble()</code>创建后的类型：</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1"></a><span class="kw">class</span>(tb)</span>
<span id="cb6-2"><a href="#cb6-2"></a><span class="co">## [1] &quot;tbl_df&quot;     &quot;tbl&quot;        &quot;data.frame&quot;</span></span></code></pre></div>
<p>tb 是<code>tbl_df</code>类型，继承于<code>tbl</code>类，而<code>tbl</code>类继承 data.frame,所以 tibble 是 data.frame 的子类型。</p>
<p><del>关于类型，继承概念不理解的话，可以跳过；或者将 python 中的面向对象编程的继承类概念迁移过来</del></p>
<p>我们多角度观察数据框 tb：</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1"></a><span class="co"># 判断是不是tibble</span></span>
<span id="cb7-2"><a href="#cb7-2"></a><span class="kw">is.tibble</span>(tb)</span>
<span id="cb7-3"><a href="#cb7-3"></a><span class="co">## [1] TRUE</span></span>
<span id="cb7-4"><a href="#cb7-4"></a></span>
<span id="cb7-5"><a href="#cb7-5"></a><span class="co"># 查看tb属性</span></span>
<span id="cb7-6"><a href="#cb7-6"></a><span class="kw">attributes</span>(tb)</span>
<span id="cb7-7"><a href="#cb7-7"></a><span class="co">## $names</span></span>
<span id="cb7-8"><a href="#cb7-8"></a><span class="co">## [1] &quot;:)&quot;   &quot; &quot;    &quot;2000&quot;</span></span>
<span id="cb7-9"><a href="#cb7-9"></a><span class="co">## </span></span>
<span id="cb7-10"><a href="#cb7-10"></a><span class="co">## $row.names</span></span>
<span id="cb7-11"><a href="#cb7-11"></a><span class="co">## [1] 1</span></span>
<span id="cb7-12"><a href="#cb7-12"></a><span class="co">## </span></span>
<span id="cb7-13"><a href="#cb7-13"></a><span class="co">## $class</span></span>
<span id="cb7-14"><a href="#cb7-14"></a><span class="co">## [1] &quot;tbl_df&quot;     &quot;tbl&quot;        &quot;data.frame&quot;</span></span>
<span id="cb7-15"><a href="#cb7-15"></a></span>
<span id="cb7-16"><a href="#cb7-16"></a><span class="co"># 查看结构</span></span>
<span id="cb7-17"><a href="#cb7-17"></a><span class="kw">str</span>(tb)</span>
<span id="cb7-18"><a href="#cb7-18"></a><span class="co">## tibble[,3] [1 × 3] (S3: tbl_df/tbl/data.frame)</span></span>
<span id="cb7-19"><a href="#cb7-19"></a><span class="co">##  $ :)  : chr &quot;smile&quot;</span></span>
<span id="cb7-20"><a href="#cb7-20"></a><span class="co">##  $     : chr &quot;space&quot;</span></span>
<span id="cb7-21"><a href="#cb7-21"></a><span class="co">##  $ 2000: chr &quot;number&quot;</span></span>
<span id="cb7-22"><a href="#cb7-22"></a></span>
<span id="cb7-23"><a href="#cb7-23"></a><span class="co"># 观察查询</span></span>
<span id="cb7-24"><a href="#cb7-24"></a><span class="kw">glimpse</span>(tb)</span>
<span id="cb7-25"><a href="#cb7-25"></a><span class="co">## Rows: 1</span></span>
<span id="cb7-26"><a href="#cb7-26"></a><span class="co">## Columns: 3</span></span>
<span id="cb7-27"><a href="#cb7-27"></a><span class="co">## $ `:)`   &lt;chr&gt; &quot;smile&quot;</span></span>
<span id="cb7-28"><a href="#cb7-28"></a><span class="co">## $ ` `    &lt;chr&gt; &quot;space&quot;</span></span>
<span id="cb7-29"><a href="#cb7-29"></a><span class="co">## $ `2000` &lt;chr&gt; &quot;number&quot;</span></span></code></pre></div>
<p>tibble 包提供了函数<code>glimpse()</code>方便观察 tibble.</p>
</div>
<div id="tibble-的数据操作" class="section level2">
<h2>tibble 的数据操作</h2>
<p>我们基本不会直接操作 tibble,仅做了解即可</p>
<ul>
<li>增加列</li>
</ul>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1"></a>df &lt;-<span class="st"> </span><span class="kw">tibble</span>(<span class="dt">x =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">3</span>,<span class="dt">y =</span> <span class="dv">3</span><span class="op">:</span><span class="dv">1</span>)</span>
<span id="cb8-2"><a href="#cb8-2"></a><span class="kw">add_column</span>(df,<span class="dt">z =</span> <span class="dv">-1</span><span class="op">:</span><span class="dv">1</span>,<span class="dt">.after =</span> <span class="st">&quot;x&quot;</span>)</span>
<span id="cb8-3"><a href="#cb8-3"></a><span class="co">## # A tibble: 3 x 3</span></span>
<span id="cb8-4"><a href="#cb8-4"></a><span class="co">##       x     z     y</span></span>
<span id="cb8-5"><a href="#cb8-5"></a><span class="co">##   &lt;int&gt; &lt;int&gt; &lt;int&gt;</span></span>
<span id="cb8-6"><a href="#cb8-6"></a><span class="co">## 1     1    -1     3</span></span>
<span id="cb8-7"><a href="#cb8-7"></a><span class="co">## 2     2     0     2</span></span>
<span id="cb8-8"><a href="#cb8-8"></a><span class="co">## 3     3     1     1</span></span></code></pre></div>
<ul>
<li>增加行</li>
</ul>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1"></a><span class="kw">add_row</span>(df,<span class="dt">x =</span> <span class="dv">100</span>,<span class="dt">y =</span> <span class="dv">100</span>)</span>
<span id="cb9-2"><a href="#cb9-2"></a><span class="co">## # A tibble: 4 x 2</span></span>
<span id="cb9-3"><a href="#cb9-3"></a><span class="co">##       x     y</span></span>
<span id="cb9-4"><a href="#cb9-4"></a><span class="co">##   &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb9-5"><a href="#cb9-5"></a><span class="co">## 1     1     3</span></span>
<span id="cb9-6"><a href="#cb9-6"></a><span class="co">## 2     2     2</span></span>
<span id="cb9-7"><a href="#cb9-7"></a><span class="co">## 3     3     1</span></span>
<span id="cb9-8"><a href="#cb9-8"></a><span class="co">## 4   100   100</span></span>
<span id="cb9-9"><a href="#cb9-9"></a><span class="kw">add_row</span>(df,<span class="dt">x =</span> <span class="dv">100</span>,<span class="dt">y =</span> <span class="dv">100</span>,<span class="dt">.before =</span><span class="dv">1</span>)</span>
<span id="cb9-10"><a href="#cb9-10"></a><span class="co">## # A tibble: 4 x 2</span></span>
<span id="cb9-11"><a href="#cb9-11"></a><span class="co">##       x     y</span></span>
<span id="cb9-12"><a href="#cb9-12"></a><span class="co">##   &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb9-13"><a href="#cb9-13"></a><span class="co">## 1   100   100</span></span>
<span id="cb9-14"><a href="#cb9-14"></a><span class="co">## 2     1     3</span></span>
<span id="cb9-15"><a href="#cb9-15"></a><span class="co">## 3     2     2</span></span>
<span id="cb9-16"><a href="#cb9-16"></a><span class="co">## 4     3     1</span></span></code></pre></div>
</div>
<div id="tibbles-and-data.frame-区别" class="section level2">
<h2>Tibbles and data.frame 区别</h2>
<p>tibble 与经典数据框 data.frame 用法主要有两个区别：打印和取子集。</p>
<div id="打印" class="section level3">
<h3>打印</h3>
<p>Tibbles 有一种精确的打印方法，它只显示前10行，以及适合屏幕的所有列，这使得处理多列数据变得更加容易，除了名字，还显示每列的类型。本质是调用<code>print)tbl()</code>函数。</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1"></a><span class="kw">tibble</span>(</span>
<span id="cb10-2"><a href="#cb10-2"></a>  <span class="dt">a =</span> lubridate<span class="op">::</span><span class="kw">now</span>() <span class="op">+</span><span class="st"> </span><span class="kw">runif</span>(<span class="fl">1e3</span>) <span class="op">*</span><span class="st"> </span><span class="dv">86400</span>,</span>
<span id="cb10-3"><a href="#cb10-3"></a>  <span class="dt">b =</span> lubridate<span class="op">::</span><span class="kw">today</span>() <span class="op">+</span><span class="st"> </span><span class="kw">runif</span>(<span class="fl">1e3</span>) <span class="op">*</span><span class="st"> </span><span class="dv">30</span>,</span>
<span id="cb10-4"><a href="#cb10-4"></a>  <span class="dt">c =</span> <span class="dv">1</span><span class="op">:</span><span class="fl">1e3</span>,</span>
<span id="cb10-5"><a href="#cb10-5"></a>  <span class="dt">d =</span> <span class="kw">runif</span>(<span class="fl">1e3</span>),</span>
<span id="cb10-6"><a href="#cb10-6"></a>  <span class="dt">e =</span> <span class="kw">sample</span>(letters, <span class="fl">1e3</span>, <span class="dt">replace =</span> <span class="ot">TRUE</span>)</span>
<span id="cb10-7"><a href="#cb10-7"></a>)</span>
<span id="cb10-8"><a href="#cb10-8"></a><span class="co">## # A tibble: 1,000 x 5</span></span>
<span id="cb10-9"><a href="#cb10-9"></a><span class="co">##    a                   b              c      d e    </span></span>
<span id="cb10-10"><a href="#cb10-10"></a><span class="co">##    &lt;dttm&gt;              &lt;date&gt;     &lt;int&gt;  &lt;dbl&gt; &lt;chr&gt;</span></span>
<span id="cb10-11"><a href="#cb10-11"></a><span class="co">##  1 2021-06-28 14:09:40 2021-07-20     1 0.717  f    </span></span>
<span id="cb10-12"><a href="#cb10-12"></a><span class="co">##  2 2021-06-28 11:44:16 2021-07-21     2 0.420  r    </span></span>
<span id="cb10-13"><a href="#cb10-13"></a><span class="co">##  3 2021-06-28 23:04:29 2021-07-22     3 0.398  j    </span></span>
<span id="cb10-14"><a href="#cb10-14"></a><span class="co">##  4 2021-06-29 06:01:37 2021-07-08     4 0.164  q    </span></span>
<span id="cb10-15"><a href="#cb10-15"></a><span class="co">##  5 2021-06-28 16:48:40 2021-06-30     5 0.426  a    </span></span>
<span id="cb10-16"><a href="#cb10-16"></a><span class="co">##  6 2021-06-29 04:49:26 2021-07-14     6 0.0133 h    </span></span>
<span id="cb10-17"><a href="#cb10-17"></a><span class="co">##  7 2021-06-28 21:22:34 2021-07-20     7 0.347  w    </span></span>
<span id="cb10-18"><a href="#cb10-18"></a><span class="co">##  8 2021-06-28 21:26:52 2021-07-23     8 0.418  r    </span></span>
<span id="cb10-19"><a href="#cb10-19"></a><span class="co">##  9 2021-06-28 21:16:37 2021-07-09     9 0.250  a    </span></span>
<span id="cb10-20"><a href="#cb10-20"></a><span class="co">## 10 2021-06-28 22:42:30 2021-07-03    10 0.673  l    </span></span>
<span id="cb10-21"><a href="#cb10-21"></a><span class="co">## # … with 990 more rows</span></span></code></pre></div>
<p>强制展示全部列：</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1"></a>nycflights13<span class="op">::</span>flights <span class="op">%&gt;%</span><span class="st"> </span></span>
<span id="cb11-2"><a href="#cb11-2"></a><span class="st">    </span><span class="kw">print</span>(<span class="dt">n =</span> <span class="dv">10</span>, <span class="dt">width =</span> <span class="ot">Inf</span>)</span>
<span id="cb11-3"><a href="#cb11-3"></a><span class="co">## # A tibble: 336,776 x 19</span></span>
<span id="cb11-4"><a href="#cb11-4"></a><span class="co">##     year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time</span></span>
<span id="cb11-5"><a href="#cb11-5"></a><span class="co">##    &lt;int&gt; &lt;int&gt; &lt;int&gt;    &lt;int&gt;          &lt;int&gt;     &lt;dbl&gt;    &lt;int&gt;          &lt;int&gt;</span></span>
<span id="cb11-6"><a href="#cb11-6"></a><span class="co">##  1  2013     1     1      517            515         2      830            819</span></span>
<span id="cb11-7"><a href="#cb11-7"></a><span class="co">##  2  2013     1     1      533            529         4      850            830</span></span>
<span id="cb11-8"><a href="#cb11-8"></a><span class="co">##  3  2013     1     1      542            540         2      923            850</span></span>
<span id="cb11-9"><a href="#cb11-9"></a><span class="co">##  4  2013     1     1      544            545        -1     1004           1022</span></span>
<span id="cb11-10"><a href="#cb11-10"></a><span class="co">##  5  2013     1     1      554            600        -6      812            837</span></span>
<span id="cb11-11"><a href="#cb11-11"></a><span class="co">##  6  2013     1     1      554            558        -4      740            728</span></span>
<span id="cb11-12"><a href="#cb11-12"></a><span class="co">##  7  2013     1     1      555            600        -5      913            854</span></span>
<span id="cb11-13"><a href="#cb11-13"></a><span class="co">##  8  2013     1     1      557            600        -3      709            723</span></span>
<span id="cb11-14"><a href="#cb11-14"></a><span class="co">##  9  2013     1     1      557            600        -3      838            846</span></span>
<span id="cb11-15"><a href="#cb11-15"></a><span class="co">## 10  2013     1     1      558            600        -2      753            745</span></span>
<span id="cb11-16"><a href="#cb11-16"></a><span class="co">##    arr_delay carrier flight tailnum origin dest  air_time distance  hour minute</span></span>
<span id="cb11-17"><a href="#cb11-17"></a><span class="co">##        &lt;dbl&gt; &lt;chr&gt;    &lt;int&gt; &lt;chr&gt;   &lt;chr&gt;  &lt;chr&gt;    &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;</span></span>
<span id="cb11-18"><a href="#cb11-18"></a><span class="co">##  1        11 UA        1545 N14228  EWR    IAH        227     1400     5     15</span></span>
<span id="cb11-19"><a href="#cb11-19"></a><span class="co">##  2        20 UA        1714 N24211  LGA    IAH        227     1416     5     29</span></span>
<span id="cb11-20"><a href="#cb11-20"></a><span class="co">##  3        33 AA        1141 N619AA  JFK    MIA        160     1089     5     40</span></span>
<span id="cb11-21"><a href="#cb11-21"></a><span class="co">##  4       -18 B6         725 N804JB  JFK    BQN        183     1576     5     45</span></span>
<span id="cb11-22"><a href="#cb11-22"></a><span class="co">##  5       -25 DL         461 N668DN  LGA    ATL        116      762     6      0</span></span>
<span id="cb11-23"><a href="#cb11-23"></a><span class="co">##  6        12 UA        1696 N39463  EWR    ORD        150      719     5     58</span></span>
<span id="cb11-24"><a href="#cb11-24"></a><span class="co">##  7        19 B6         507 N516JB  EWR    FLL        158     1065     6      0</span></span>
<span id="cb11-25"><a href="#cb11-25"></a><span class="co">##  8       -14 EV        5708 N829AS  LGA    IAD         53      229     6      0</span></span>
<span id="cb11-26"><a href="#cb11-26"></a><span class="co">##  9        -8 B6          79 N593JB  JFK    MCO        140      944     6      0</span></span>
<span id="cb11-27"><a href="#cb11-27"></a><span class="co">## 10         8 AA         301 N3ALAA  LGA    ORD        138      733     6      0</span></span>
<span id="cb11-28"><a href="#cb11-28"></a><span class="co">##    time_hour          </span></span>
<span id="cb11-29"><a href="#cb11-29"></a><span class="co">##    &lt;dttm&gt;             </span></span>
<span id="cb11-30"><a href="#cb11-30"></a><span class="co">##  1 2013-01-01 05:00:00</span></span>
<span id="cb11-31"><a href="#cb11-31"></a><span class="co">##  2 2013-01-01 05:00:00</span></span>
<span id="cb11-32"><a href="#cb11-32"></a><span class="co">##  3 2013-01-01 05:00:00</span></span>
<span id="cb11-33"><a href="#cb11-33"></a><span class="co">##  4 2013-01-01 05:00:00</span></span>
<span id="cb11-34"><a href="#cb11-34"></a><span class="co">##  5 2013-01-01 06:00:00</span></span>
<span id="cb11-35"><a href="#cb11-35"></a><span class="co">##  6 2013-01-01 05:00:00</span></span>
<span id="cb11-36"><a href="#cb11-36"></a><span class="co">##  7 2013-01-01 06:00:00</span></span>
<span id="cb11-37"><a href="#cb11-37"></a><span class="co">##  8 2013-01-01 06:00:00</span></span>
<span id="cb11-38"><a href="#cb11-38"></a><span class="co">##  9 2013-01-01 06:00:00</span></span>
<span id="cb11-39"><a href="#cb11-39"></a><span class="co">## 10 2013-01-01 06:00:00</span></span>
<span id="cb11-40"><a href="#cb11-40"></a><span class="co">## # … with 336,766 more rows</span></span></code></pre></div>
</div>
<div id="子集" class="section level3">
<h3>子集</h3>
<p>使用 $ 或者 [[ 提取子集，[[ 可以通过位置或名字提取，$ 仅仅通过名字提取，如下所示：</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1"></a>df &lt;-<span class="st"> </span><span class="kw">tibble</span>(</span>
<span id="cb12-2"><a href="#cb12-2"></a>  <span class="dt">x =</span> <span class="kw">runif</span>(<span class="dv">5</span>),</span>
<span id="cb12-3"><a href="#cb12-3"></a>  <span class="dt">y =</span> <span class="kw">rnorm</span>(<span class="dv">5</span>)</span>
<span id="cb12-4"><a href="#cb12-4"></a>)</span></code></pre></div>
<p>通过使用 $ 和名称提取</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1"></a>df<span class="op">$</span>x</span>
<span id="cb13-2"><a href="#cb13-2"></a><span class="co">## [1] 0.4098776 0.2930897 0.7433447 0.2816653 0.9714782</span></span></code></pre></div>
<p>使用[[ 提取</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1"></a><span class="co"># 名称</span></span>
<span id="cb14-2"><a href="#cb14-2"></a>df[[<span class="st">&quot;x&quot;</span>]]</span>
<span id="cb14-3"><a href="#cb14-3"></a><span class="co">## [1] 0.4098776 0.2930897 0.7433447 0.2816653 0.9714782</span></span>
<span id="cb14-4"><a href="#cb14-4"></a><span class="co"># 数字索引</span></span>
<span id="cb14-5"><a href="#cb14-5"></a>df[[<span class="dv">1</span>]]</span>
<span id="cb14-6"><a href="#cb14-6"></a><span class="co">## [1] 0.4098776 0.2930897 0.7433447 0.2816653 0.9714782</span></span></code></pre></div>
<p>在管道符中使用，我们需要使用特殊占位符 .:</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1"></a>df <span class="op">%&gt;%</span><span class="st"> </span>.<span class="op">$</span>x</span>
<span id="cb15-2"><a href="#cb15-2"></a><span class="co">## [1] 0.4098776 0.2930897 0.7433447 0.2816653 0.9714782</span></span>
<span id="cb15-3"><a href="#cb15-3"></a>df <span class="op">%&gt;%</span><span class="st"> </span>.[[<span class="st">&quot;x&quot;</span>]]</span>
<span id="cb15-4"><a href="#cb15-4"></a><span class="co">## [1] 0.4098776 0.2930897 0.7433447 0.2816653 0.9714782</span></span></code></pre></div>
<p>与 data.frame 相比，tibbles更严格，从不进行部分匹配，也就是我们尝试访问不存在的列，将不返回结果。</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1"></a>df &lt;-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">ab=</span><span class="dv">1</span><span class="op">:</span><span class="dv">10</span>)</span>
<span id="cb16-2"><a href="#cb16-2"></a><span class="co"># 尝试访问不存在的列 也可以通过模糊匹配返回</span></span>
<span id="cb16-3"><a href="#cb16-3"></a>df<span class="op">$</span>a </span>
<span id="cb16-4"><a href="#cb16-4"></a><span class="co">##  [1]  1  2  3  4  5  6  7  8  9 10</span></span></code></pre></div>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1"></a>df &lt;-<span class="st"> </span><span class="kw">tibble</span>(<span class="dt">ab =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">10</span>)</span>
<span id="cb17-2"><a href="#cb17-2"></a><span class="co"># 无法返回结果</span></span>
<span id="cb17-3"><a href="#cb17-3"></a>df<span class="op">$</span>a</span>
<span id="cb17-4"><a href="#cb17-4"></a><span class="co">## NULL</span></span></code></pre></div>
</div>
</div>
<div id="tibble-转换成-data.frame" class="section level2">
<h2>tibble 转换成 data.frame</h2>
<p>一些较旧的函数不适用于tibbles,我们可以使用 as.data.frame()将tibble 转回 data.frame:</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1"></a><span class="kw">class</span>(<span class="kw">as.data.frame</span>(df))</span>
<span id="cb18-2"><a href="#cb18-2"></a><span class="co">## [1] &quot;data.frame&quot;</span></span></code></pre></div>
</div>
<div id="参考资料" class="section level2">
<h2>参考资料</h2>
<p>建议阅读 R for Data Science 的<a href="https://r4ds.had.co.nz/tibbles.html">tibbles</a>章节，本文内容大部分来源该章节。</p>
<p>或者可以通过手册学习 tibble</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1"></a><span class="kw">vignette</span>(<span class="st">&quot;tibble&quot;</span>)</span></code></pre></div>
<ol style="list-style-type: decimal">
<li>张丹大神: <a href="http://blog.fens.me/r-tibble/" class="uri">http://blog.fens.me/r-tibble/</a></li>
</ol>
</div>
